﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace DMM
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string drive = "";
        public MainWindow()
        {
            InitializeComponent();
            main = this;
            drivebox.ItemsSource = Environment.GetLogicalDrives();
        }
        internal static MainWindow main;
        internal string Status
        {
            get { return prglbl.Content.ToString(); }
            set { Dispatcher.Invoke(new Action(() => { prglbl.Content = value; })); }
        }
        internal double prgstat
        {
            get { return prgbar.Value; }
            set { Dispatcher.Invoke(new Action(() => { prgbar.Value = value; })); }
        }
        internal ItemCollection files
        {
            get { return lstfiles.Items; }
            set { Dispatcher.Invoke(new Action(() => { lstfiles.Items.RemoveAt(0); })); }
        }
        internal int finished
        {
            get { return finishedtasks; }
            set { Dispatcher.Invoke(new Action(() => { finishedtasks = value; })); }
        }
        internal int failures
        {
            get { return fail; }
            set { Dispatcher.Invoke(new Action(() => { fail = value; })); }
        }
        internal Button convertbutton
        {
            get { return convertbtn; }
            set { Dispatcher.Invoke(new Action(() => { convertbtn.IsEnabled = true; MessageBox.Show("Conversion Completed with " + fail + "errors."); })); }
        }
        internal int all
        {
            get { return alltasks; }
            set { Dispatcher.Invoke(new Action(() => { alltasks = value; })); }
        }
        public int alltasks = 0;
        public int finishedtasks = 0;
        public int fail = 0;
        private void openbtn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openfiles = new OpenFileDialog();
            openfiles.Filter = "GameCube Games(*.ISO;*.GCM)|*.ISO;*.GCM";
            openfiles.CheckFileExists = true;
            openfiles.Multiselect = true;
            if (openfiles.ShowDialog() == true)
            {
                foreach (string file in openfiles.FileNames)
                {
                    lstfiles.Items.Add(file);
                    MainWindow.main.all = MainWindow.main.all + 1;
                    prgbar.Maximum = MainWindow.main.all;
                    prglbl.Content = finishedtasks.ToString() + "/" + MainWindow.main.all.ToString();
                }
            }
        }

        private void convertbtn_Click(object sender, RoutedEventArgs e)
        {
            convertbtn.IsEnabled = false;
            drive = drivebox.SelectedItem.ToString();
            if (!System.IO.Directory.Exists(drivebox.SelectedItem.ToString() + "games"))
            {
                System.IO.Directory.CreateDirectory(drivebox.SelectedItem.ToString() + "games");
            }
            System.IO.File.Copy("discex.exe", drive + "games\\discex.exe");
            System.IO.File.Copy("libeay32.dll", drive + "games\\libeay32.dll");
            System.IO.File.Copy("libWiiSharp.dll", drive + "games\\libWiiSharp.dll");
            System.IO.File.Copy("msvcp100.dll", drive + "games\\msvcp100.dll");
            System.IO.File.Copy("msvcr100.dll", drive + "games\\msvcr100.dll");
            Worker workerObject = new Worker();
            System.Threading.Thread workerThread = new System.Threading.Thread(workerObject.DoWork);

            workerThread.Start();
        }

        private void advbtn_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(drivebox.SelectedItem.ToString());
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            dminstall dm = new dminstall();
            dm.ShowDialog();
        }
    }
    public class Worker
    {
        public void DoWork()
        {
            while (!_shouldStop)
            {
                while (MainWindow.main.finished < MainWindow.main.all)
                {
                    Console.WriteLine("Now Converting " + MainWindow.main.files.GetItemAt(0).ToString());
                    System.Diagnostics.ProcessStartInfo pinfo = new System.Diagnostics.ProcessStartInfo();
                    var proc = new System.Diagnostics.Process
                    {
                        StartInfo = new System.Diagnostics.ProcessStartInfo
                        {
                            FileName = "discex.exe",
                            Arguments = "-a \"" + MainWindow.main.files.GetItemAt(0) + "\"",
                            WorkingDirectory = MainWindow.main.drive + "games"
                            
                        }
                    };
                    
                    proc.Start();
                    proc.WaitForExit();
                    switch (proc.ExitCode)
                    {
                        case 0:
                            Console.WriteLine("Finished Converting of " + MainWindow.main.files.GetItemAt(0));
                            MainWindow.main.finished = MainWindow.main.finished + 1;
                            MainWindow.main.files = MainWindow.main.files;
                            MainWindow.main.Status = MainWindow.main.finished + "/" + MainWindow.main.all;
                            MainWindow.main.prgstat = MainWindow.main.finished;
                            break;
                        default:
                            Console.WriteLine("Conversion of " + MainWindow.main.files.GetItemAt(0) + " failed");
                            MainWindow.main.finished = MainWindow.main.finished + 1;
                            MainWindow.main.files = MainWindow.main.files;
                            MainWindow.main.Status = MainWindow.main.finished + "/" + MainWindow.main.all;
                            MainWindow.main.prgstat = MainWindow.main.finished;
                            break;
                    }
                }
                RequestStop();
                MainWindow.main.convertbutton = MainWindow.main.convertbutton;
            }
            Console.WriteLine("worker thread: terminating gracefully.");
            System.IO.File.Delete(MainWindow.main.drive + "games\\discex.exe");
            System.IO.File.Delete(MainWindow.main.drive + "games\\libeay32.dll");
            System.IO.File.Delete(MainWindow.main.drive + "games\\libWiiSharp.dll");
            System.IO.File.Delete(MainWindow.main.drive + "games\\msvcp100.dll");
            System.IO.File.Delete(MainWindow.main.drive + "games\\msvcr100.dll");
        }
        public void RequestStop()
        {
            _shouldStop = true;
        }
        private volatile bool _shouldStop;
    }
}
